package c.x.jy.admin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import c.x.jy.dao.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import c.x.jy.dao.entity.CmsCategoryEntity;
import c.x.jy.service.service.CmsCategoryService;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;



/**
 * 栏目表
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-05-22 16:39:17
 */
@RestController
@RequestMapping("cms/cmscategory")
public class CmsCategoryController extends AbstractController {
    @Autowired
    private CmsCategoryService cmsCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("cms:cmscategory:list")
    public R list(@RequestParam Map<String, Object> params){
     	if(getUser().getBusinessId()>0) {
    		params.put("businessId", getUser().getBusinessId());
    	}
        PageUtils page = cmsCategoryService.queryPage(params);

        return R.put(page);
    }

    @GetMapping("/allList")
       @RequiresPermissions("cms:cmscategory:list")
    public R alllist(@RequestParam Map<String, Object> params){

        return R .put(   cmsCategoryService.list());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("cms:cmscategory:info")
    public R info(@PathVariable("id") Integer id){
		CmsCategoryEntity cmsCategory = cmsCategoryService.getById(id);

        return R.put(cmsCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("cms:cmscategory:save")
    public R save(@RequestBody CmsCategoryEntity cmsCategory){

//        if(getUser().getBusinessId()==0) {
//            return R.error("请用商家帐号添加");
//        }

        if(cmsCategory.getId()==null) {
            cmsCategory.setCreateBy(( (SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());
            cmsCategory.setCreateDate(new Date());
            cmsCategory.setBusinessId(getUser().getBusinessId());
            cmsCategoryService.save(cmsCategory);

        }else {
            cmsCategory.setUpdateBy(( ( SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());
            cmsCategory.setUpdateDate(new Date());
            cmsCategory.setBusinessId(null);
            cmsCategoryService.updateById(cmsCategory);
        }


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("cms:cmscategory:update")
    public R update(@RequestBody CmsCategoryEntity cmsCategory){
		cmsCategoryService.updateById(cmsCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("cms:cmscategory:delete")
    public R delete(@PathVariable("id") Integer id){
		cmsCategoryService.removeByIds(Arrays.asList(id));

        return R.ok();
    }

}

